//
//  HMHomeRetweetView.swift
//  Weibo24
//
//  Created by apple on 17/3/30.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
import SnapKit
import YYText

//  自定义转发微博视图
class HMHomeRetweetView: UIView {
    
    //  MARK: ---  懒加载控件
//    fileprivate lazy var contentLabel: UILabel = {
//        let label = UILabel(fontSize: 13, textColor: UIColor.lightGray, text: "窗前明月光窗前明月光窗前明月光窗前明月光窗前明月光窗前明月光窗前明月光窗前明月光窗前明月光窗前明月光窗前明月光窗前明月光窗前明月光窗前明月光窗前明月光窗前明月光")
//        //  多行显示
//        label.numberOfLines = 0
//        return label
//    }()
    
    fileprivate lazy var contentLabel: YYLabel = {
        let label = YYLabel()
        //  最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HomeCellMargin
        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    
    //  配图
    fileprivate lazy var pictureView: HMHomePictureView = {
        let picView = HMHomePictureView()
        picView.backgroundColor = self.backgroundColor
        return picView
    }()
    //  记录转发微博的底部约束
    var retweetBottomConstraint: Constraint?
    
    //  绑定数据
    var statusViewModel: HMStatusViewModel? {
        didSet {
            //  转发微博内容
//            contentLabel.text = statusViewModel?.retweetContent
            //  转发微博的富文本
            contentLabel.attributedText = statusViewModel?.retweetAttributedString
            
            //  1. 先要把之前的转发微博的底部约束卸载掉,然后根据判断重写创建转发微博的底边约束+间距, 要保证之前的转发微博底部是没有约束的,否则会有冲突
            retweetBottomConstraint?.deactivate()
            //  判断是否有配图
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
                //  重新创建约束
                pictureView.isHidden = false
                self.snp.makeConstraints({ (make) in
                    retweetBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                //  有配图
                pictureView.picUrls = picUrls
                
            } else {
                //  没有配图
                pictureView.isHidden = true
                //  重新创建约束
                self.snp.makeConstraints({ (make) in
                    retweetBottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        addSubview(contentLabel)
        addSubview(pictureView)
        contentLabel.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentLabel)
            //  暂时写死
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //  关键约束
        self.snp.makeConstraints { (make) in
            retweetBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
        //  设置点击高亮文字需要执行的闭包
        contentLabel.highlightTapAction = { (label, labelAttributedStr, range, rect) in
            //  label: 点击视图
            //  labelAttributedStr: 视图显示的富文本
            //  range: 点击高亮文字的范围
            //  rect: 点击整体高亮文字的区域
            let result = (labelAttributedStr.string as NSString).substring(with: range)
            print(result)
        }
    }

}
